package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.data.core.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.vo.eduHouseDetail.EduHouseDetailKeyVO;
import com.jwsoft.manager.common.vo.eduHouseDetail.EduHouseDetailQueryVO;
import com.jwsoft.manager.common.vo.eduHouseDetail.EduHouseDetailSearchVO;
import com.jwsoft.manager.common.vo.eduHouseDetail.EduHouseDetailVO;
import com.jwsoft.manager.core.dao.model.EduHouseDetail;
import com.jwsoft.manager.core.dao.service.EduHouseDetailService;
import com.jwsoft.manager.core.integration.EduHouseDetailIntegration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 房产信息表业务实现类
 *
 * @author wangtao
 * @since 2022-12-01
 */
@ApiService(funcCode = "eduHouseDetail", title = "房产信息表")
public class EduHouseDetailIntegrationImpl implements EduHouseDetailIntegration {

    @Autowired
    public EduHouseDetailService eduHouseDetailService;

    @Override
    @OpApi(funcCode = "eduHouseDetail0001", title = "房产信息表分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduHouseDetailVO> getList(EduHouseDetailQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<EduHouseDetail> list = eduHouseDetailService.list();
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduHouseDetail> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduHouseDetailVO> resultList = Convert.toList(EduHouseDetailVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduHouseDetail0002", title = "房产信息表根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduHouseDetailVO getById(EduHouseDetailKeyVO vo) {
        EduHouseDetail entity = eduHouseDetailService.getById(vo.getHouseDetailId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduHouseDetailVO result = Convert.convert(EduHouseDetailVO.class, entity);
        return result;
    }

    /**
     * 保存前统一校验
     */
    @Override
    @OpApi(funcCode = "eduHouseDetail0003", title = "房产信息表保存前校验", funcType = FuncTypeEnum.other, checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduHouseDetailVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "eduHouseDetail0004", title = "房产信息表新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduHouseDetail0004",
            operationName = "房产信息表新增",
            dataType = "houseDetailId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduHouseDetailVO add(EduHouseDetailVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduHouseDetail entity = Convert.convert(EduHouseDetail.class, vo);
        eduHouseDetailService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setHouseDetailId(entity.getHouseDetailId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduHouseDetail0005", title = "房产信息表修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduHouseDetail0005",
            operationName = "房产信息表修改",
            dataType = "houseDetailId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduHouseDetailVO edit(EduHouseDetailVO vo) {
        if (ObjectUtils.isEmpty(vo.getHouseDetailId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduHouseDetail oldEntity = eduHouseDetailService.getById(vo.getHouseDetailId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduHouseDetail entity = Convert.convert(EduHouseDetail.class, vo);
        eduHouseDetailService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduHouseDetail0006", title = "房产信息表根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduHouseDetail0006",
            operationName = "房产信息表删除",
            dataType = "houseDetailId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduHouseDetailKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getHouseDetailId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduHouseDetail entity = eduHouseDetailService.getById(vo.getHouseDetailId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduHouseDetailService.removeById(vo.getHouseDetailId());
    }

    @Override
    @OpApi(funcCode = "eduHouseDetail0007", title = "不动产权号查询", funcType = FuncTypeEnum.other)
    public PageInfo<EduHouseDetailVO> queryHouseId(EduHouseDetailSearchVO vo) {
        //开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            vo.setPageNum(1);
            vo.setPageSize(20);
        }
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        String realProperty = vo.getSearchText();
        QueryWrapper<EduHouseDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .select(EduHouseDetail::getHouseDetailId, EduHouseDetail::getHouseId, EduHouseDetail::getRealProperty)
                .like(
                        !ObjectUtils.isEmpty(realProperty),
                        EduHouseDetail::getRealProperty,
                        realProperty
                );
        List<EduHouseDetail> list = eduHouseDetailService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        PageInfo<EduHouseDetail> pageInfo = new PageInfo<>(list);
        List<EduHouseDetailVO> voList = Convert.toList(EduHouseDetailVO.class, pageInfo.getList());
        return PagerUtil.parsePagerVo(voList, pageInfo);
    }
}
